//
//  LSZWordBankDetailsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/25.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZWordBankDetailsVC.h"

@interface LSZWordBankDetailsVC ()

@end

@implementation LSZWordBankDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"词库详情";
    self.cellBool = NO;
    [self.TableviewDetails registerClass:[LSZWordDetailsHeaderTableViewCell class] forCellReuseIdentifier:@"LSZWordDetailsHeaderTableViewCell"];
    [self.TableviewDetails registerClass:[LSZWordHeaderCell class] forHeaderFooterViewReuseIdentifier:@"LSZWordHeaderCell"];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PrayerWords" ofType:@"plist"];
    _ArrayCount = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == self.sectionChoose && isOpen[indexPath.section]) {
        NSArray * detailArrat = self.ArrayCount[self.strWhat];
        NSDictionary * dic = detailArrat[indexPath.section];
        NSArray * arrayValue = dic.allValues;
        CGFloat height = [self initWithStrHeigh:[NSString stringWithFormat:@"%@",arrayValue[0]]];
        return height;//这个数据是可变的
    }else{
        return 0;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSArray * detailArrat = self.ArrayCount[self.strWhat];
    return  detailArrat.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == self.sectionChoose && isOpen[section]) {
        return 1;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LSZWordHeaderCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZWordHeaderCell"];
    if (!cell) {
        cell = [[LSZWordHeaderCell alloc]initWithReuseIdentifier:@"LSZWordHeaderCell"];
    }
    NSArray * detailArrat = self.ArrayCount[self.strWhat];
    NSDictionary * dic = detailArrat[section];
    NSArray * arrayKey = dic.allKeys;
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",arrayKey[0]];
    cell.buttChoose.tag = section;
    [cell.buttChoose addTarget:self action:@selector(clickHeader:) forControlEvents:UIControlEventTouchUpInside];
    if(section != self.sectionChoose){
        isOpen[section] = NO;
    }
    cell.imageViewRotating.transform = isOpen[section]?CGAffineTransformMakeRotation(M_PI_2):CGAffineTransformIdentity;
    cell.backgroundColor = RGBColor(239, 239, 244);
    return cell;
}
-(void)clickHeader :(UIButton *)button
{
    isOpen[button.tag] = !isOpen[button.tag];
    self.sectionChoose = button.tag;
    [self.TableviewDetails reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZWordDetailsHeaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZWordDetailsHeaderTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[LSZWordDetailsHeaderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LSZWordDetailsHeaderTableViewCell"];
    }
    NSArray * detailArrat = self.ArrayCount[self.strWhat];
    NSDictionary * dic = detailArrat[indexPath.section];
    NSArray * arrayValue = dic.allValues;
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",arrayValue[0]];
    CGFloat height = [self initWithStrHeigh:[NSString stringWithFormat:@"%@",arrayValue[0]]];
    CGRect rect2 = cell.labelTitle.frame;
    rect2.size.height = height;
    cell.labelTitle.frame = rect2;
     if (self.view.height == 667 || self.view.height == 736) {
         cell.labelTitle.font = FONT_18;
     }else{
         cell.labelTitle.font = FONT_16;
     }
    cell.backgroundColor = RGBColor(240, 240, 240);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)initWithStrHeigh :(NSString * )str
{
    if (self.view.height == 667 || self.view.height == 736) {
        CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(self.view.width-30, self.view.height*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_18 forKey:NSFontAttributeName] context:nil];
        return labTitleRect.size.height+40;
    }else{
        CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(self.view.width-30, self.view.height*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_16 forKey:NSFontAttributeName] context:nil];
        return labTitleRect.size.height+40;
    }
}
-(UITableView *)TableviewDetails
{
    if (_TableviewDetails == nil) {
        _TableviewDetails= [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _TableviewDetails.delegate = self;
        _TableviewDetails.dataSource = self;
        _TableviewDetails.estimatedRowHeight = 0;
        _TableviewDetails.estimatedSectionHeaderHeight = 0;
        _TableviewDetails.estimatedSectionFooterHeight = 0;
        [self.view addSubview:_TableviewDetails];
    }
    return _TableviewDetails;
}
@end
